package org.lex.notepad.ui.search;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import org.lex.swing.widgets.EnhancedDialog;


public class ReplaceDialog extends EnhancedDialog {
	private JPanel pane = null;
	private JLabel findLabel = null;
	private JTextField findTextField = null;
	private JButton findButton = null;
	private JCheckBox matchWholeWordCheckBox = null;
	private JCheckBox matchCaseCheckBox = null;
	private JButton closeButton = null;
	private JPanel gluePane = null;
	private JLabel replaceLabel = null;
	private JTextField replaceTextField = null;
	private JButton replaceButton = null;
	private JButton replaceAllButton = null;

	public static void main(String[] args) {
		new ReplaceDialog(null).setVisible(true);
	}

	public ReplaceDialog(Window owner) {
		super(owner);
		this.initialize();
		this.setDefaultButton(this.getFindButton());
	}

	public void cancel() {
		dispose();
	}

	private void initialize() {
		this.setSize(420, 215);
		this.setTitle("Replace");
		this.setContentPane(getPane());
	}

	private JPanel getPane() {
		if (pane == null) {
			GridBagConstraints gridBagConstraints4 = new GridBagConstraints();
			gridBagConstraints4.gridx = 3;
			gridBagConstraints4.insets = new Insets(0, 5, 0, 11);
			gridBagConstraints4.fill = GridBagConstraints.HORIZONTAL;
			gridBagConstraints4.gridy = 2;
			GridBagConstraints gridBagConstraints3 = new GridBagConstraints();
			gridBagConstraints3.gridx = 3;
			gridBagConstraints3.insets = new Insets(0, 5, 5, 11);
			gridBagConstraints3.fill = GridBagConstraints.HORIZONTAL;
			gridBagConstraints3.gridy = 1;
			GridBagConstraints gridBagConstraints22 = new GridBagConstraints();
			gridBagConstraints22.fill = GridBagConstraints.BOTH;
			gridBagConstraints22.gridy = 1;
			gridBagConstraints22.weightx = 1.0;
			gridBagConstraints22.insets = new Insets(0, 5, 5, 0);
			gridBagConstraints22.gridwidth = 2;
			gridBagConstraints22.gridx = 1;
			GridBagConstraints gridBagConstraints12 = new GridBagConstraints();
			gridBagConstraints12.gridx = 0;
			gridBagConstraints12.fill = GridBagConstraints.HORIZONTAL;
			gridBagConstraints12.anchor = GridBagConstraints.WEST;
			gridBagConstraints12.insets = new Insets(3, 12, 5, 0);
			gridBagConstraints12.gridwidth = 1;
			gridBagConstraints12.gridy = 1;
			replaceLabel = new JLabel();
			replaceLabel.setText("Replace with");
			GridBagConstraints gridBagConstraints7 = new GridBagConstraints();
			gridBagConstraints7.gridx = 0;
			gridBagConstraints7.gridwidth = 4;
			gridBagConstraints7.fill = GridBagConstraints.BOTH;
			gridBagConstraints7.weightx = 10.0;
			gridBagConstraints7.weighty = 10.0;
			gridBagConstraints7.gridy = 4;
			GridBagConstraints gridBagConstraints6 = new GridBagConstraints();
			gridBagConstraints6.gridx = 3;
			gridBagConstraints6.fill = GridBagConstraints.HORIZONTAL;
			gridBagConstraints6.insets = new Insets(0, 5, 11, 11);
			gridBagConstraints6.gridy = 5;
			GridBagConstraints gridBagConstraints21 = new GridBagConstraints();
			gridBagConstraints21.gridx = 1;
			gridBagConstraints21.anchor = GridBagConstraints.WEST;
			gridBagConstraints21.insets = new Insets(0, 5, 0, 0);
			gridBagConstraints21.fill = GridBagConstraints.HORIZONTAL;
			gridBagConstraints21.gridwidth = 2;
			gridBagConstraints21.gridy = 3;
			GridBagConstraints gridBagConstraints11 = new GridBagConstraints();
			gridBagConstraints11.gridx = 1;
			gridBagConstraints11.anchor = GridBagConstraints.WEST;
			gridBagConstraints11.insets = new Insets(5, 5, 0, 0);
			gridBagConstraints11.fill = GridBagConstraints.HORIZONTAL;
			gridBagConstraints11.gridwidth = 2;
			gridBagConstraints11.gridy = 2;
			GridBagConstraints gridBagConstraints2 = new GridBagConstraints();
			gridBagConstraints2.gridx = 3;
			gridBagConstraints2.insets = new Insets(12, 5, 5, 11);
			gridBagConstraints2.fill = GridBagConstraints.HORIZONTAL;
			gridBagConstraints2.gridy = 0;
			GridBagConstraints gridBagConstraints1 = new GridBagConstraints();
			gridBagConstraints1.fill = GridBagConstraints.BOTH;
			gridBagConstraints1.gridy = 0;
			gridBagConstraints1.weightx = 10.0;
			gridBagConstraints1.gridwidth = 2;
			gridBagConstraints1.anchor = GridBagConstraints.WEST;
			gridBagConstraints1.insets = new Insets(12, 5, 5, 0);
			gridBagConstraints1.gridx = 1;
			GridBagConstraints gridBagConstraints = new GridBagConstraints();
			gridBagConstraints.gridx = 0;
			gridBagConstraints.gridwidth = 1;
			gridBagConstraints.anchor = GridBagConstraints.WEST;
			gridBagConstraints.insets = new Insets(12, 12, 5, 11);
			gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
			gridBagConstraints.gridy = 0;
			findLabel = new JLabel();
			findLabel.setText("Find what");
			pane = new JPanel();
			pane.setLayout(new GridBagLayout());
			pane.add(findLabel, gridBagConstraints);
			pane.add(getFindTextField(), gridBagConstraints1);
			pane.add(getFindButton(), gridBagConstraints2);
			pane.add(getMatchWholeWordCheckBox(), gridBagConstraints11);
			pane.add(getMatchCaseCheckBox(), gridBagConstraints21);
			pane.add(getCloseButton(), gridBagConstraints6);
			pane.add(getGluePane(), gridBagConstraints7);
			pane.add(replaceLabel, gridBagConstraints12);
			pane.add(getReplaceTextField(), gridBagConstraints22);
			pane.add(getReplaceButton(), gridBagConstraints3);
			pane.add(getReplaceAllButton(), gridBagConstraints4);
		}
		return pane;
	}

	private JTextField getFindTextField() {
		if (findTextField == null) {
			findTextField = new JTextField();
		}
		return findTextField;
	}

	private JButton getFindButton() {
		if (findButton == null) {
			findButton = new JButton();
			findButton.setText("Find next");
		}
		return findButton;
	}

	private JCheckBox getMatchWholeWordCheckBox() {
		if (matchWholeWordCheckBox == null) {
			matchWholeWordCheckBox = new JCheckBox();
			matchWholeWordCheckBox.setText("Match whole word only");
		}
		return matchWholeWordCheckBox;
	}

	private JCheckBox getMatchCaseCheckBox() {
		if (matchCaseCheckBox == null) {
			matchCaseCheckBox = new JCheckBox();
			matchCaseCheckBox.setText("Match case");
		}
		return matchCaseCheckBox;
	}

	private JButton getCloseButton() {
		if (closeButton == null) {
			closeButton = new JButton();
			closeButton.setText("Close");
			closeButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					cancel();
				}
			});
		}
		return closeButton;
	}

	private JTextField getReplaceTextField() {
		if (replaceTextField == null) {
			replaceTextField = new JTextField();
		}
		return replaceTextField;
	}

	private JButton getReplaceButton() {
		if (replaceButton == null) {
			replaceButton = new JButton();
			replaceButton.setText("Replace");
		}
		return replaceButton;
	}

	private JButton getReplaceAllButton() {
		if (replaceAllButton == null) {
			replaceAllButton = new JButton();
			replaceAllButton.setText("Replace All");
		}
		return replaceAllButton;
	}

	private JPanel getGluePane() {
		if (gluePane == null) {
			gluePane = new JPanel();
		}
		return gluePane;
	}
}
